﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using WPFStudy.Data;
using WPFStudy.Extensions;
using WPFStudy.Helper;

namespace WPFStudy.ViewModel
{
    /// <summary>
    /// DataGrid转DataTable
    /// </summary>
    public class DataGridToDataTableVM: ViewModelBase
    {
        private DataGrid _dataGrid;

        public DataGridToDataTableVM(DataGrid dataGrid):this()
        {
            _dataGrid = dataGrid;
        }

        public DataGridToDataTableVM()
        {
            DataList = new ObservableCollection<DataGridData>()
            {
                new DataGridData()
                {
                    No = 1,
                    Name ="Name1",
                    Money = 0.1M,
                },
                new DataGridData()
                {
                    No = 2,
                    Name ="Name2",
                    CreateTime = DateTime.Now.AddDays(2),
                    Money = 2.1M,
                },
                new DataGridData()
                {
                    No = 3,
                    Name ="Name3",
                    CreateTime = DateTime.Now.AddDays(3),
                    Money = 3.3M,
                },
                new DataGridData()
                {
                    No = 4,
                    Name ="Name4",
                    CreateTime = DateTime.Now.AddDays(4),
                    Money = 4.4M,
                },
                new DataGridData()
                {
                    No = 5,
                    Name ="Name5",
                    CreateTime = DateTime.Now.AddDays(5),
                    Money = 5.5M,
                },
                new DataGridData()
                {
                    No = 6,
                    Name ="Name14",
                    CreateTime = DateTime.Now.AddDays(6),
                    Money = 6.6M,
                }
            };
        }

        #region 数据源
        /// <summary>
        /// 数据源
        /// </summary>
        private ObservableCollection<DataGridData> _dataList;

        /// <summary>
        /// 数据源
        /// </summary>
        public ObservableCollection<DataGridData> DataList
        {
            get => _dataList;
            set => Set(ref _dataList, value);
        }
        #endregion 数据源


        #region 导出

        /// <summary>
        /// 导出
        /// </summary>
        private RelayCommand _exportCmd;

        /// <summary>
        /// 导出
        /// </summary>
        public RelayCommand ExportCmd
        {
            get
            {
                if (_exportCmd == null)
                {
                    _exportCmd = new RelayCommand(Export);
                }

                return _exportCmd;
            }
            set { _exportCmd = value; }

        }

        /// <summary>
        /// 导出
        /// </summary>
        private void Export()
        {

            DataTable dt = new DataTable();
            int columnCount = 0;
            foreach (var dgc in _dataGrid.Columns)
            {
                bool isExport = DataGridExtensions.GetIsExportExcel(dgc);
                if (isExport)
                {
                    columnCount++;
                    DataColumn dc = new DataColumn();
                    var dataValue = DataGridExtensions.GetExcelCellValue(dgc);
                    //BindingBase binding = DataGridExtensions.GetExcelCellValueBinding(dgc);
                    string header = DataGridExtensions.GetExcelColumnHeader(dgc);
                    if (dt.Columns.Contains(header))
                    {
                        header = $"{header}_{columnCount}";
                    }
                    dc.ColumnName = header;
                    dc.DataType = typeof(object);
                    dt.Columns.Add(dc);
                }
            }
            for (int i = 0; i < _dataGrid.Items.Count; i++)
            {
                DataRow dataRow = dt.NewRow();
                columnCount = 0;
                int columnIndex = 0;
                var vi = _dataGrid.Items[i];
                foreach (var dgc in _dataGrid.Columns)
                {
                    bool isExport = DataGridExtensions.GetIsExportExcel(dgc);
                    if (isExport)
                    {
                        columnCount++;
                        DataColumn dc = new DataColumn();
                        string header = DataGridExtensions.GetExcelColumnHeader(dgc);
                        if (string.IsNullOrEmpty(header))
                        {
                            header = $"col_{columnCount}";
                        }
                        if (header.Contains("编号") || header.Contains("模板"))
                        {
                            Console.WriteLine("");
                        }
                        BindingBase binding = null;// DataGridExtensions.GetExcelCellValueBinding(dgc);
                        if (binding == null)
                        {
                            binding = ReflectionHelper.GetObjectPropertyValue<DataGridColumn, BindingBase>(dgc, nameof(Binding));
                            if (binding == null)
                            {
                                binding = ReflectionHelper.GetObjectPropertyValue<DataGridColumn, BindingBase>(dgc, nameof(DataGridColumn.ClipboardContentBinding));
                            }
                        }
                        if (binding != null)
                        {
                            object columnValue = null;
                            TextBlock tb = new TextBlock();
                            tb.DataContext = vi;
                            tb.SetBinding(TextBlock.TextProperty, binding);
                            columnValue = tb.Text;
                            dataRow[header] = columnValue;
                        }
                    }
                    columnIndex++;
                }
                dt.Rows.Add(dataRow);
            }
            object res = dt;
        }

        #endregion 导出
    }
}
